/**
 * Created by csy on 2017/11/20.
 */
var validateWapper;
var $alert = require("libs/alert");
var isSale = require('libs/user-type').isSale;
require(['jquery','easyui','js-path/jquery-validation-wrapper'],function($){
    appendOption('api/v1/p/crm/warehouse/query?warehouseType=A',$("#warehouseCode"),"warehouseCode","warehouseName",{warehouseCode:'',warehouseName:'全部'});
    $.parser.parse();
    tableInit();
    formValideInit();
    //查询按钮点击事件
    $("#main_search").on("click",function () {
        $('#main_table').datagrid('load',queryParams());
    });
    $(document).on("click",'a[name=audit]',function () {
        $("#audit").modal("show");
        var index = $(this).data('index');
        var row = $('#main_table').datagrid('getData').rows[index];
        if(row){
            $('#id').val(row.id);
            $("#audit .modal-body").find('span').eq(0).text(row.warehouseName);
            $("#audit .modal-body").find('span').eq(1).text(row.entName);
            $("#audit .modal-body").find('span').eq(2).text(row.currency);
            $("#audit .modal-body").find('span').eq(3).text(row.consumeReason);
            $("#audit .modal-body").find('span').eq(4).text(row.amount);
            $("#audit .modal-body").find('span').eq(5).text(row.factAmount);
            $("#main_form input:radio[name='auditStatus']").eq(0).prop("checked",true);
            $('#main_form textarea').val('');
        }
    });
    $("#submit_btn").on('click',function() {
        $('#main_form').submit();
    });
    $('#audit').on('hidden.bs.modal', function (e) {
        $("#main_form input:radio[name='auditStatus']").eq(1).prop("checked",false);
    });
    $(document).on("click",'a[name=look]',function () {
        window.parent.tabOpen($(this).attr('ref'),"扣费详情",true,true);
    });
});

function tableInit() {
    $("#main_table").datagrid({
        url:'api/v1/p/mms/service/order/consume/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        rownumbers:true,
        singleSelect: true,
        emptyMsg : "暂无数据",
        queryParams:queryParams(),
        columns:[[ {
            field : 'code',
            title : '扣费服务编号',
            width:"12%",
            formatter: function(value,row,index){
                return "<a name='look' href='javascript:;' ref='service-detail?id="+row.id+"'>"+value+"</a>";
            }
        },{
            field : 'warehouseName',
            title : '海外仓',
            width:"12%"
        },{
            field : 'entName',
            title : '客户名称',
            width:"15%"
        },{
            field:'relEntityCode',
            title:'关联单号',
            width:"12%"
        },{
            field:'currency',
            title:'币制',
            width:"5%"
        },{
            field:'amount',
            title:'扣费金额',
            width:"6%"
        },{
            field:'factAmount',
            title:'成本金额',
            width:"6%"
        },{
            field:'confirmeStatus',
            title:'确认状态',
            width:"6%",
            formatter:function (value,row,index) {
                switch (value){
                    case "unconfirmed": return "待确认";
                    case "confirmed": return "已确认";
                    case "confirme_not_pass": return "未同意";
                    default : return "";
                }
            },
            styler: function(value,row,index){
                if (value =='confirme_not_pass'){
                    return 'color:red;';
                }
            }
        },{
            field:'auditStatus',
            title:'审核状态',
            width:"6%",
            formatter:function (value,row,index) {
                switch (value){
                    case "unaudited": return "待审核";
                    case "audited": return "已审核";
                    case "audit_not_pass": return "未通过";
                    default : return "";
                }
            },
            styler: function(value,row,index){
                if (value =='audit_not_pass'){
                    return 'color:red;';
                }
            }
        },{
            field:'createdDt',
            title:'创建时间',
            width:"12%"
        },{
            field:'opt',
            title:'操作',
            hidden:isSale,
            width:"10%",
            formatter:function (value,row,index) {
                return "<a href='javascript:;' name='audit' data-index='"+index+"'>审核</a>";
            }
        }
        ]],
        loadFilter:function(data){
            if(data.code=='200'){
                return data.data;
            }else{
                return [];
            }
        },
        toolbar:"#tb"
    });
}
function queryParams() {
    var d1 = $('#startDt').datebox('getValue');
    var d2 = $('#endDt').datebox('getValue');
    var data =
        {
            source:'audit',
            code:$('#code').val(),
            warehouseCode:$('#warehouseCode').combobox('getValue'),
            entName:$('#entName').val(),
            relEntityCode:$('#relEntityCode').val(),
            processStatus:'unaudited',
            startDate:d1?$('#startDt').data('utc0'):'',
            endDate:d2?$('#endDt').data('utc0'):''
        };
    return data;
}
//添加参数
function appendOption(url,$select,valueField,textField,all) {
    $select.combobox({
        url:url,
        valueField:valueField,
        textField:textField,
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift(all);
                return result.data;
            }
        }
    })
}
function formValideInit() {
    validateWapper = $('#main_form').validateWapper({
        rules: {
            auditReply: {maxlength: 200}
        }, submitHandler: function () {
            var formData = $('#main_form').serializeObject();
            $.ajax({
                url: $('#main_form').attr('action'),
                type: 'post',
                data: JSON.stringify(formData),
                contentType: 'application/json;charset=utf-8'
            })
                .done(function (res) {
                    if (res.code == 200) {
                        $("#audit").modal("hide");
                        $('#main_table').datagrid('load');
                    } else {
                        $alert(res.message);
                    }
                });
        }
    });
}